package comandos;

import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;

/**
 * Trabalho DAS - 2014/2015
 * @author a21210380, a21190325, a21210392
 */
public class GestorDeComandos implements IGestorComandos {
    private final ObservableList<IComando> undo = FXCollections.observableArrayList();
    private final ObservableList<IComando> redo = FXCollections.observableArrayList();
    
    @Override
    public void executeComando(IComando c) {
        c.execute();
        undo.add(c);
    }
    
    @Override
    public void undo() { 
        if(!undo.isEmpty()) {
            IComando get = undo.remove(undo.size()-1);
            get.undo();
            redo.add(get);
        }
    }
    
    @Override
    public void redo() {
        if(!redo.isEmpty()) {
            IComando get = redo.remove(redo.size()-1);
            get.execute();
            undo.add(get);
        }
    }
    
    @Override
    public void clearUndoList() {
        undo.clear();
    }
    
    @Override
    public void clearRedoList() {
        redo.clear();
    }
    
    @Override
    public void clearAll() {
        undo.clear();
        redo.clear();
    }
    
    @Override
    public void addUndoListener(ListChangeListener<IComando> listener) {
        undo.addListener(listener);
    }
    
    @Override
    public void addRedoListener(ListChangeListener<IComando> listener) {
        redo.addListener(listener);
    }
    
    @Override
    public ObservableList<IComando> getUndoList() {
        return undo;
    }

    @Override
    public boolean isUndoEmpty() {
        return undo.isEmpty();
    }

    @Override
    public boolean isRedoEmpty() {
        return redo.isEmpty();
    }
}
